package pl.com.photogetter;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;


/**
 * @author Piotr Maślany
 */
public class PropertyWizard extends JDialog implements ActionListener {
	
	private String Version = PhotoDownloader.Version;
	private Container cp;
	private JLabel complexProcessingLabel, separatorLabel;
	private JButton abortButton, saveButton, sourceBrowseButton, targetBrowseButton;
	private JPanel southPanel, processingPanel, headerPanel, separatorPanel;
	private Properties dbProperties;
	private JCheckBox complexProcessing;
	private JTextField sourceTextField, targetTextField, separatorField;
	private GUI _parent;
	
	public PropertyWizard(Frame parent, Properties props) {
		
		super(parent, true);
		dbProperties = props;
		_parent = (GUI)parent;

		initComponents();
		pack();
		
	}

	/**
	 * 
	 */
	private void initComponents() 
	{
		cp = this.getContentPane();
		cp.setLayout(new BorderLayout());
		
		southPanel = new JPanel(new FlowLayout());
		processingPanel = new JPanel(new FlowLayout());
		separatorPanel = new JPanel(new FlowLayout());
		
		sourceTextField = new JTextField(30);
		sourceTextField.setEditable(true);
		sourceTextField.setText(dbProperties.getProperty("SOURCEDIR") == null ? "" : dbProperties.getProperty("SOURCEDIR"));
		targetTextField = new JTextField(30);
		targetTextField.setText(dbProperties.getProperty("PHOTODIR") == null ? "" : dbProperties.getProperty("PHOTODIR"));
		targetTextField.setEditable(true);
		
		targetBrowseButton = new JButton("Katalog docelowy");
		targetBrowseButton.setToolTipText("Ustaw katalog docelowy");
		targetBrowseButton.addActionListener(this);
		sourceBrowseButton = new JButton("Katalog źródłowy");
		sourceBrowseButton.setToolTipText("Ustaw katalog źródłowy");
		sourceBrowseButton.addActionListener(this);

		complexProcessingLabel = new JLabel("Zagnieżdżanie pełne");
		complexProcessing = new JCheckBox("tak / nie");
		String ndb = dbProperties.getProperty("COMPLEX"); 
		if(ndb != null && ndb.matches("TRUE")) {
			
			complexProcessing.setSelected(true);
		}
		else {
			
			complexProcessing.setSelected(false);
		}
		processingPanel.add(complexProcessingLabel);
		processingPanel.add(complexProcessing);
		
		separatorLabel = new JLabel("Separator w nazwie katalogów");
		separatorField = new JTextField(5);
		separatorField.setEditable(true);
		separatorField.setText(dbProperties.getProperty("SEPARATOR") == null ? "" : dbProperties.getProperty("SEPARATOR"));
		separatorPanel.add(separatorLabel);
		separatorPanel.add(separatorField);
		
		headerPanel = new JPanel(new BorderLayout());

		JPanel headerNorthPanel = new JPanel(new FlowLayout());
		JPanel headerSouthPanel = new JPanel(new FlowLayout());

		headerNorthPanel.add(sourceBrowseButton);
		headerNorthPanel.add(sourceTextField);

		headerSouthPanel.add(targetBrowseButton);
		headerSouthPanel.add(targetTextField);
		
		
		headerPanel.add(headerNorthPanel, BorderLayout.NORTH);
		headerPanel.add(headerSouthPanel, BorderLayout.SOUTH);
		
		cp.add(headerPanel, BorderLayout.NORTH);
		cp.add(separatorPanel,BorderLayout.WEST);		
		
		abortButton = new JButton("Anuluj");
		abortButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				abortButtonActionPerformed(e);
			}
		});
		saveButton = new JButton("Zapisz ustawienia");
		saveButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveButtonActionPerformed(e);
			}
		});
		
		cp.add(processingPanel,BorderLayout.CENTER);
		
		southPanel.add(abortButton);
		southPanel.add(saveButton);
		cp.add(southPanel, BorderLayout.SOUTH);
	}

	protected void saveButtonActionPerformed(ActionEvent e) 
	{
		if (separatorField.getText() != null && separatorField.getText().length() > 1)
		{
			JOptionPane.showMessageDialog(
					this,
					"Separator powinien mieć jeden znak",
					"Separator powinien mieć jeden znak",
					JOptionPane.ERROR_MESSAGE,
					null);
		}
		dbProperties.setProperty("PHOTODIR", targetTextField.getText());
		dbProperties.setProperty("SOURCEDIR", sourceTextField.getText());
		dbProperties.setProperty("SEPARATOR", separatorField.getText());
		
		if (complexProcessing.isSelected()) {
			
			dbProperties.setProperty("COMPLEX", "TRUE");	
		}
		else {
			
			dbProperties.setProperty("COMPLEX", "FALSE");
		}
		
		try 
		{
			dbProperties.store(new BufferedOutputStream(new FileOutputStream(dbProperties.getProperty("PROPERTIESFILE"))),"PhotoGetter Properties File");
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		this.setVisible(false);
		
		try {
			this.finalize();
		} catch (Throwable e2) {
			e2.printStackTrace();
		}
		
	}

	protected void abortButtonActionPerformed(ActionEvent e) {
		this.setVisible(false);
		
		try {
			this.finalize();
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) 
	{
		if (e.getActionCommand() == "Katalog źródłowy") {
			sourceTextField.setText(this._parent.browseSource(this));
		} else if (e.getActionCommand() == "Katalog docelowy") {
			targetBrowseButtonActionPerformed(e);

		}
	}
	
	protected void targetBrowseButtonActionPerformed(ActionEvent e) 
	{
		targetTextField.setText(this._parent.browseSource(this));
		dbProperties.setProperty("PHOTODIR", targetTextField.getText());
	}

}
